package com.pet.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.server.domain.entity.Pet;
import com.pet.server.mapper.PetMapper;
import com.pet.server.service.IPetService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class PetServiceImpl implements IPetService {
  private final PetMapper petMapper;

  @Override
  public boolean AddPet(Pet pet) {
    int i = petMapper.insertOrUpdate(pet);

    return i > 0;
  }

  @Override
  public boolean DelPet(int id) {
    int i = petMapper.deleteById(id);

    return i > 0;
  }

  @Override
  public boolean Update(Pet pet) {
    int i = petMapper.insertOrUpdate(pet);

    return i > 0;
  }

  @Override
  public Pet listAll(int i) {
    return petMapper.selectById(i);
  }

  @Override
  public List<Pet> getAllByPage(int pageNo, int pageSize, int petMasterId) {
    QueryWrapper<Pet> queryWrapper = new QueryWrapper<>();

    queryWrapper.eq("fk_master_id", petMasterId);

    Page<Pet> petPage = petMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
    List<Pet> list = petPage.getRecords();

    return list;
  }

  @Override
  public List<Pet> getPetBySelect(int pageNo, int pageSize, Pet pet) {
    QueryWrapper<Pet> queryWrapper = new QueryWrapper<>();

    //        queryWrapper.eq("fk_master_id", pet.getMasterId());

    if ((pet.getPetName() != null) && !("".equalsIgnoreCase(pet.getPetName()))) {
      queryWrapper.like("pet_name", pet.getPetName());
    }

    if ((pet.getPetCategory() != null) && !("".equalsIgnoreCase(pet.getPetCategory()))) {
      queryWrapper.like("category", pet.getPetCategory());
    }

    if ((pet.getPetCharacter() != null) && !("".equalsIgnoreCase(pet.getPetCharacter()))) {
      queryWrapper.like("character", pet.getPetCharacter());
    }

    if ((pet.getPetColour() != null) && !("".equalsIgnoreCase(pet.getPetColour()))) {
      queryWrapper.like("colour", pet.getPetColour());
    }

    if ((pet.getPetType() != null) && !("".equalsIgnoreCase(pet.getPetType()))) {
      queryWrapper.like("type", pet.getPetType());
    }

    Page<Pet> petPage = petMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
    List<Pet> list = petPage.getRecords();

    return list;
  }
}

// ~ Formatted by Jindent --- http://www.jindent.com
